<?php

if(!defined('IN_SYS')) exit('Access Denied');

class classlist_controller extends admincp{

	public function __construct(){
		parent::__construct();
	}

	public function index(){
		if(!$this->currentYumingId){
			self::redirect("?m=yuming&a=select");
		}

		$tagsname = gp("tagsname");
		$list = false;
		if(!empty($this->currentYumingId)){
			$list = db::select("select c.* from classlist as c where c.yuming_id='".$this->currentYumingId."' order by c.weight,id");
			if (empty($list)) {
				$addsql = "";
				foreach ($this->sysclass as $ename => $title) {
					$addsql .= (empty($addsql)? "" : ",") . "('{$this->currentYumingId}','$ename','$title','1')";
				}
				if ($addsql) {
					$addsql = "insert into classlist(yuming_id,ename,title,ifsys)values".$addsql;
					db::query($addsql);
				}
				$list = db::select("select c.* from classlist as c where c.yuming_id='".$this->currentYumingId."' order by c.weight,id");
			}
		}
		require self::tpl('classlist');
	}

	public function add(){ 
		if(IS_POST){
			
			$v = gp("title,ename,weight");

			if(empty($v['title']) || empty($v['ename'])){
				self::json( array('status' => 0, 'errMsg' => '请填写栏目名称和英文标记'));
			}

			$check = db::find("select * from classlist where yuming_id='{$this->currentYumingId}' and (title='$v[title]' or ename='$v[ename]')");
			if ($check) {
				self::json( array('status' => 0, 'errMsg' => '栏目名称或标记已存在！'));
			}
//			if(!validate::alpha($v['ename'])){
//				self::json( array('status' => 0, 'errMsg' => '标记存在不允许的字符'));
//			}
			db::query("insert into classlist (yuming_id, ename, title, weight, ifsys, status ) 
				values ('{$this->currentYumingId}','$v[ename]', '$v[title]', '$v[weight]', '0', '1')");
			//存入文章内容
			$content_id = db::insert_id();
			if($content_id){
				$nav_cache_id = 'nav_'.$this->currentYumingId;
				cache::delete($nav_cache_id);
				self::json( array('status' => 1, 'msg' => '添加成功', 'redirect' => '?m=classlist&a=index&yuming_id='.$this->currentYumingId));
			}else{
				self::json( array('status' => 0, 'errMsg' => '添加失败'));
			}
		}else{
			require self::tpl("classlist_add");
		}
	}

	public function edit(){
		$id = gp('id');
		
		if(IS_POST) {
			$v = gp("title,ename,weight");

			if(empty($v['title']) || empty($v['ename'])){
				self::json( array('status' => 0, 'errMsg' => '请填写栏目名称和英文标记'));
			}

			$check = db::find("select * from classlist where yuming_id='{$this->currentYumingId}' and id<>'$id' and (title='$v[title]' or ename='$v[ename]')");
			if ($check) {
				self::json( array('status' => 0, 'errMsg' => '栏目名称或标记已存在！'));
			}
//			if(!validate::alpha($v['ename'])){
//				self::json( array('status' => 0, 'errMsg' => '标记存在不允许的字符'));
//			}
			$rs = db::query("update classlist set ename='$v[ename]', title='$v[title]', weight='$v[weight]' where id='$id'");
			if($rs){
				$nav_cache_id = 'nav_'.$this->currentYumingId;
				cache::delete($nav_cache_id);
				self::json( array('status' => 1, 'msg' => '修改成功', 'redirect' => '?m=classlist&a=index&yuming_id='.$this->currentYumingId));
			}else{
				self::json( array('status' => 0, 'errMsg' => '修改失败'));
			}

		}else{
			$r = db::find("select * from classlist where id='$id' ");
			require self::tpl("classlist_edit");
		}
	}

	public function del(){
		$yuming_id = $this->currentYumingId;
		$id = gp('id');
		$class = db::getField("select ename from classlist where id='$id'");
		db::query("delete from content_{$yuming_id} where class='$class'");
		
		db::query("delete from classlist where id='$id' ");

		$nav_cache_id = 'nav_'.$this->currentYumingId;
		cache::delete($nav_cache_id);

		self::redirect('?m=classlist&yuming_id='.$yuming_id, "删除成功");
	}

	public function stop() {
		$yuming_id = gp("yuming_id");
		$id = gp('id');
		//$class = db::getField("select ename from classlist where id='$id'");
		db::query("update classlist set status=0 where id='$id' ");
		//db::query("delete from content_{$yuming_id} where class='$class'");

		$nav_cache_id = 'nav_'.$this->currentYumingId;
		cache::delete($nav_cache_id);

		self::redirect('?m=classlist&yuming_id='.$yuming_id, "操作成功");
	}

	public function open() {
		$yuming_id = $this->currentYumingId;
		$id = gp('id');
		db::query("update classlist set status=1 where id='$id' ");

		$nav_cache_id = 'nav_'.$this->currentYumingId;
		cache::delete($nav_cache_id);

		self::redirect('?m=classlist&yuming_id='.$yuming_id, "操作成功");
	}

	public function weight() {
		$yuming_id = $this->currentYumingId;
		$id = gp('id');
		$weight = gp("weight");
		db::query("update classlist set weight='$weight' where id='$id' ");

		$nav_cache_id = 'nav_'.$this->currentYumingId;
		cache::delete($nav_cache_id);

		$result = array('status'=>1);
		self::json($result);
	}

	public function autoclass() {
		$ymlist = $this->allyuming;
		foreach ($ymlist as $key => $value) {
			$sid = $value['id'];
			$classlist = db::find("select c.* from classlist as c where c.yuming_id='$sid'");
			if (empty($classlist)) {
				$addsql = "";
				foreach ($this->sysclass as $ename => $title) {
					$addsql .= (empty($addsql)? "" : ",") . "('$sid','$ename','$title','1')";
				}
				if ($addsql) {
					$addsql = "insert into classlist(yuming_id,ename,title,ifsys)values".$addsql;
					db::query($addsql);
					dump($addsql);
				}
			}
		}
	}

}


